﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("YUIAspNet");

YUIAspNet.Menu = function(element) {
    YUIAspNet.Menu.initializeBase(this, [element]);
}

YUIAspNet.Menu.prototype = {
    initialize: function() {
        YUIAspNet.Menu.callBaseMethod(this, 'initialize');
    },

    initializeYUIControl: function() {
        var config = this._buildConfig();
        
        this._attachHandlers();

        if (this.get_MenuType() == "vertical" || this.get_MenuType() == "dynamic") {
             var menuPosition = "static"; // visible by default.
            if (this.get_MenuType() == "dynamic")
                menuPosition = "dynamic";
                
            config.position = menuPosition;
            this._yuiControl = new YAHOO.widget.Menu(this.get_MenuContainerID(), config);

            if (this.get_FocusOnShow())
                this._yuiControl.subscribe("show", yuiMenu.focus);
            if (this.get_ToggleElementID()) {
                var elemLocation = Sys.UI.DomElement.getBounds($get(this.get_ToggleElementID()));

                var menu = this._yuiControl;
                elemLocation.y += elemLocation.height;
                
                YAHOO.util.Event.addListener(this.get_ToggleElementID(), "click", function() {
                    menu.cfg.setProperty("x", elemLocation.x);
                    menu.cfg.setProperty("y", elemLocation.y);
                    menu.show();
                },
                    null, this._yuiControl);
            }
        }
        else if (this.get_MenuType() == "horizontal") {
            this._yuiControl = new YAHOO.widget.MenuBar(this.get_MenuContainerID(), config);
            
        }
        else if (this.get_MenuType() == "context") {
            config.trigger = this.get_element().id;
            config.itemdata = this.get_Items();
            
            //force lazyload for context menu, otherwise it won't work...
            config.lazyload = true;
            config.container = this.get_element().id;

            this._yuiControl = new YAHOO.widget.ContextMenu(this.get_element().id + "_ContextMenu",config);
        }
        
                        
        //this._yuiControl.setItemGroupTitle("Yahoo! PIM", 0); 
        //this._yuiControl.setItemGroupTitle("Yahoo! PIM2", 1); 
        
        //context menu is lazy + items must be added with config
        if(this.get_MenuType() != "context")
        {
            this._yuiControl.addItems(this.get_Items());
            this._yuiControl.render(this.get_MenuContainerID() + "_menuItems");
        }

    },

    _buildConfig: function() {
        var config = {};

        config.disabled = !this.get_Enabled(),
        config.fixedcenter = this.get_ShowCentered();
        if(this.get_ClassName())
            config.classname = this.get_ClassName();
        
        if(this.get_Effect())
        {
            config.effect = this._getEffect();
        }
        
        config.autosubmenudisplay = this.get_AutoSubMenuDisplay();
        config.clicktohide = this.get_ClickToHide();
        config.constraintoviewport = this.get_ConstrainToViewport();
        config.hidedelay = this.get_HideDelay();
        config.iframe = this.get_UseIFrame();
        config.keepopen = this.get_KeepOpen();
        config.maxheight = this.get_MaxHeight();
        config.minscrollheight = this.get_MinScrollHeight();
        config.preventcontextoverlap = this.get_PreventContextOverlap();
        config.scrollincrement = this.get_ScrollIncrement();
        config.showshadow = this.get_ShowShadow();
        config.submenuhidedelay = this.get_SubMenuHideDelay();
        config.lazyload = this.get_LazyLoad();
             
        return config;    
    },
    
    _attachHandlers: function() {
        for(var idx = 0; idx<this.get_Items().length; idx++) {
            if(this.get_Items()[idx].AutoPostback || this.get_Items()[idx].OnClientClick)
                this._attachHandler(this.get_Items()[idx]);
        }
    },
    
    _attachHandler: function(menuItem) {
         var onClickHandler = false;
         var sender = this;
            
        if (menuItem.AutoPostback) {
            if (menuItem.OnClientClick) {
                var customClick = menuItem.OnClientClick;
                onClickHandler = function() {
                    var args = { cancel: false, item: menuItem };
                    customClick(sender, args);

                    if (!args.cancel)
                        sender.DoPostback(menuItem.value || menuItem.text);
                }
            }
            else
                onClickHandler = function() { sender.DoPostback(menuItem.value || menuItem.text); }
        }
        else if (menuItem.OnClientClick) {
            onClickHandler = function() {
                var args = { cancel: false, item: menuItem, yuiitem: arguments[2] };
                menuItem.OnClientClick(sender, args);
            };
        }

        var yuiHandler = null;

        if (onClickHandler)
            yuiHandler = { fn: onClickHandler };
            
        menuItem.onclick = yuiHandler;
    },

    get_ClassName: function() {
        return this._className;
    },
    set_ClassName: function(className) {
        this._className = className;
    },

    get_AutoSubMenuDisplay: function() {
        return this._autoSubMenuDisplay;
    },
    set_AutoSubMenuDisplay: function(autoSubMenuDisplay) {
        this._autoSubMenuDisplay = autoSubMenuDisplay;
    },
    
    get_ClickToHide: function() {
        return this._clickToHide;
    },
    set_ClickToHide: function(clickToHide) {
        this._clickToHide = clickToHide;
    },
  
    get_ConstrainToViewport: function() {
        return this._constrainToViewport;
    },
    set_ConstrainToViewport: function(constrainToViewport) {
        this._constrainToViewport = constrainToViewport;
    },
    
    get_HideDelay: function() {
        return this._hideDelay;
    },
    set_HideDelay: function(hideDelay) {
        this._hideDelay = hideDelay;
    },
  
    get_UseIFrame: function() {
        return this._useIFrame;
    },
    set_UseIFrame: function(useIFrame) {
        this._useIFrame = useIFrame;
    },
  
    get_KeepOpen: function() {
        return this._keepOpen;
    },
    set_KeepOpen: function(keepOpen) {
        this._keepOpen = keepOpen;
    },
  
    get_MaxHeight: function() {
        return this._maxHeight;
    },
    set_MaxHeight: function(maxHeight) {
        this._maxHeight = maxHeight;
    },
  
    get_MinScrollHeight: function() {
        return this._minScrollHeight;
    },
    set_MinScrollHeight: function(minScrollHeight) {
        this._minScrollHeight = minScrollHeight;
    },
  
    get_PreventContextOverlap: function() {
        return this._preventContextOverlap;
    },
    set_PreventContextOverlap: function(preventContextOverlap) {
        this._preventContextOverlap = preventContextOverlap;
    },
  
    get_ScrollIncrement: function() {
        return this._scrollIncrement;
    },
    set_ScrollIncrement: function(scrollIncrement) {
        this._scrollIncrement = scrollIncrement;
    },
  
    get_ShowShadow: function() {
        return this._showShadow;
    },
    set_ShowShadow: function(showShadow) {
        this._showShadow = showShadow;
    },
  
    get_SubMenuHideDelay: function() {
        return this._subMenuHideDelay;
    },
    set_SubMenuHideDelay: function(subMenuHideDelay) {
        this._subMenuHideDelay = subMenuHideDelay;
    },
  
    get_LazyLoad: function() {
        return this._lazyLoad;
    },
    set_LazyLoad: function(lazyLoad) {
        this._lazyLoad = lazyLoad;
    },

    get_MenuType: function() {
        return this._menuType;
    },
    set_MenuType: function(menuType) {
        this._menuType = menuType;
    },

    get_Items: function() {
        return this._items;
    },
    set_Items: function(items) {
        this._items = items;
    },

    get_Enabled: function() {
        return this._enabled;
    },
    set_Enabled: function(enabled) {
        this._enabled = enabled;
    },

    get_ShowCentered: function() {
        return this._showCentered;
    },
    set_ShowCentered: function(showCentered) {
        this._showCentered = showCentered;
    },

    get_ToggleElementID: function() {
        return this._toggleElementID;
    },
    set_ToggleElementID: function(toggleElementID) {
        this._toggleElementID = toggleElementID;
    },

    get_FocusOnShow: function() {
        return this._focusOnShow;
    },
    set_FocusOnShow: function(focusOnShow) {
        this._focusOnShow = focusOnShow;
    },

    get_MenuContainerID: function() {
        return this._menuContainerID;
    },
    set_MenuContainerID: function(menuContainerID) {
        this._menuContainerID = menuContainerID;
    },
    
    get_Groups: function() {
        return this._groups;
    },
    set_Groups: function(groups) {
        this._groups = groups;
    },

    _getConvertedMenuItems: function(itemList, groups) {
        if (!itemList || itemList.length == 0)
            return null;
        
        var yuiMenuItemList = [];
        var yuiMenuGroups = [];
        
        for (var idx = 0; idx < itemList.length; idx++) {
            yuiMenuItemList[idx] = this._convertMenuItem(itemList[idx]);

            var childs = this._getConvertedMenuItems(itemList[idx].Items);

            if (childs) {
                yuiMenuItemList[idx].submenu = { id: this.get_element().id + itemList[idx].Value + "_submenu", itemdata: childs };
            }
        }
       
        return yuiMenuItemList;
    },

    _convertMenuItem: function(menuItem) {
        var onClickHandler = null;
        var sender = this;
        
        var yuiItem =
            {
                text: menuItem.Text,
                value: menuItem.Value,
                selected: menuItem.Selected,
                checked: menuItem.Checked,
                disabled: !menuItem.Enabled,
                url: menuItem.NavigateUrl,
                target: menuItem.Target,
                classname: menuItem.CssClass                
            };

        if (menuItem.AutoPostback) {
            if (menuItem.OnClientClick) {
                var customClick = menuItem.OnClientClick;
                onClickHandler = function() {
                    var args = { cancel: false, item: menuItem };
                    window[customClick](sender, args);

                    if (!args.cancel)
                        sender.DoPostback(menuItem.Value);
                }
            }
            else
                onClickHandler = function() { sender.DoPostback(menuItem.Value); }
        }
        else if (menuItem.OnClientClick) {
            onClickHandler = function() {
                var args = { cancel: false, item: menuItem, yuiitem: arguments[2] };
                window[menuItem.OnClientClick](sender, args);
            };
        }

        var yuiHandler = null;

        if (onClickHandler)
            yuiHandler = { fn: onClickHandler };
            
        yuiItem.onclick = yuiHandler;

        return yuiItem;
    },

    add_onChange: function(handler) {
        this.get_events().addHandler("onChange", handler);
    },

    remove_onChange: function(handler) {
        this.get_events().removeHandler("onChange", handler);
    },

    dispose: function() {
        $clearHandlers(this.get_element());

        //Add custom dispose actions here
        YUIAspNet.Menu.callBaseMethod(this, 'dispose');
    }
}
YUIAspNet.Menu.registerClass('YUIAspNet.Menu', YUIAspNet.BaseControl);
